const { ccclass, property } = cc._decorator;
import { resourcePath, EPoolName } from "../../constants";
import { testData } from "../testData";
import { PoolUtil } from "../../../vv/utils/PoolUtil";
import { ResourceItemBean } from "../bean/ResourceItemBean";
import { IconResourcePath } from "../bean/Config";
import { ToastUtils } from "../../../vv/utils/ToastUtils";
import { AudioUtil } from "../../../vv/utils/AudioUtil";
import { Config } from "../bean/Config";

//游戏逻辑
@ccclass
export default class gameContent extends cc.Component {
    private bgSprite: cc.Sprite = null; //游戏背景
    private resourceNode: cc.Node = null; //资源挂点
    private refreshTimeBtn: cc.Node = null; //资源刷新按钮
    private weatherNode: cc.Node = null; //天气节点

    public curMapId: number = 1; //当前地图ID

    onLoad() {
        this.bgSprite = cc.find("Canvas/bg").getComponent(cc.Sprite);
        this.resourceNode = this.node.getChildByName("resource");
        this.weatherNode = this.node.getChildByName("weather");
        this.refreshTimeBtn = cc.find("Canvas/ui/refreshTimeBtn");

        this.onEvents();
    }

    //事件绑定
    private onEvents() {
        this.refreshTimeBtn.on(cc.Node.EventType.TOUCH_END, this.onRefreshTimeBtnClick, this);
    }

    protected onDestroy() {
        this.offEvents();
    }

    //事件注销
    private offEvents() {
        this.refreshTimeBtn.off(cc.Node.EventType.TOUCH_END, this.onRefreshTimeBtnClick, this);
    }

    //在地图上刷新资源物品，地图资源做本地缓存
    public initResourceItemPool() {
        let data = this.getResourceItemData(this.curMapId);

        cc.resources.load(resourcePath.resourceItemPrefab, cc.Prefab, (err, prefab: any) => {
            PoolUtil.inst.initPool(EPoolName.resourceItem, prefab);
            for (let item of data) {
                //刷新几率
                if (item.rare < 1) {
                    if (item.rare < Math.random()) {
                        continue;
                    }
                }
                this.instantiateItem(item);
            }
        });

        //赋值刷新按钮
        let refreshTime = ResourceItemBean.inst.getResourceRefreshTimeCache(this.curMapId);
        if (Number(refreshTime) > 0) {
            vv.getScript(this.refreshTimeBtn).showCountdown(this.curMapId, refreshTime);
        }
    }

    public doRefresh(){
        let refreshTime = ResourceItemBean.inst.getResourceRefreshTimeCache(this.curMapId);
        if (Number(refreshTime) == 0){
            
        }
    }

    //根据地图ID重置刷新资源
    public resetResourceItem(mapId: number) {
        this.curMapId = mapId;
        let iconPath = IconResourcePath.BG + mapId;
        cc.resources.load(iconPath, cc.SpriteFrame, (err, obj: any) => {
            this.bgSprite.spriteFrame = obj;
        });

        let nodes = this.resourceNode.children;
        PoolUtil.inst.put(EPoolName.resourceItem, nodes);

        let data = this.getResourceItemData(this.curMapId);

        for (let item of data) {
            //刷新几率
            if (item.rare < 1) {
                if (item.rare < Math.random()) {
                    continue;
                }
            }
            this.instantiateItem(item);
        }

        //天气
        this.weatherControl();
    }

    private getResourceItemData(mapId: number) {
        let data: Array<any> = ResourceItemBean.inst.getCache(mapId);
        if (data == null || data.length == 0) {
            data = testData.getResourceData(mapId);
            ResourceItemBean.inst.setCache(mapId, data);
        }
        return data;
    }

    private instantiateItem(item: any) {
        let node = PoolUtil.inst.get(EPoolName.resourceItem);
        this.resourceNode.addChild(node);
        node.position = new cc.Vec3(item.pos[0], item.pos[1]);
        node.getComponent("resourceItem").init(item.itemId, item.count, item.remark, item.lv, item.rare, item.title, item.rowId, item.mapId);
    }

    //TODO 地图刷新倒计时
    private onRefreshTimeBtnClick() {
        //TODO 判断刷新时间
        let refreshTime = ResourceItemBean.inst.getResourceRefreshTimeCache(this.curMapId);
        if (Number(refreshTime) == 0) {
            //可以刷新
            ResourceItemBean.inst.cleanCache(this.curMapId);
            this.resetResourceItem(this.curMapId);
            let arr: Array<any> = testData.getResourceRefreshTime();
            let time = arr.find((x) => x.mapId == this.curMapId).refreshTime;
            ResourceItemBean.inst.updateResourceRefreshTimeCache(this.curMapId, time);
            vv.getScript(this.refreshTimeBtn).showCountdown(this.curMapId, time);
        } else {
            ToastUtils.show("未到时间刷新");
        }
    }

    //下雨天
    private weatherRainy() {
        let rainNode = this.weatherNode.getChildByName("rainy");
        rainNode.active = true;
        AudioUtil.inst.playMusic(resourcePath.sound_weather_rainy);
    }

    private weatherControl() {
        let rainNode = this.weatherNode.getChildByName("rainy");
        rainNode.active = false;
        AudioUtil.inst.stopMusic();

        if (this.curMapId == 1 && Math.random() > 0.5) {
            this.weatherRainy(); //下雨
        }
    }
}
